package com.morehair.service.wx.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.wx.WxCommentCountBo;
import com.morehair.bean.bo.wx.WxCommentListBo;
import com.morehair.bean.pojo.Comment;
import com.morehair.bean.pojo.CommentExample;
import com.morehair.bean.vo.admin.OrderDetailByUserVo;
import com.morehair.bean.vo.wx.WxCommentCountVo;
import com.morehair.bean.vo.wx.WxCommentListVo;
import com.morehair.mapper.CommentMapper;
import com.morehair.mapper.UserMapper;
import com.morehair.service.wx.WxCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxCommentServiceImp implements WxCommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    public static List<String> stringToList(String preStr) {
        List<String> list = new ArrayList<>();
        if (preStr.length() > 4) {
            String[] strArr = preStr.substring(1, preStr.length() - 1).split(", ");
            // 数组转化为List
            for (String str : strArr) {
                list.add(str.substring(1, str.length() - 1));
            }
        }
        return list;
    }
    @Override
    public WxCommentCountVo getCommentCount(WxCommentCountBo commentCountBo) {
        WxCommentCountVo commentCountVo = new WxCommentCountVo();
        //用CommentExample做
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(commentCountBo.getValueId())
                .andTypeEqualTo(commentCountBo.getType());
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        criteria.andHasPictureEqualTo(true);
        List<Comment> commentsHasPics = commentMapper.selectByExample(commentExample);
        commentCountVo.setAllCount(comments.size());
        commentCountVo.setHasPicCount(commentsHasPics.size());
        return commentCountVo;
    }

    @Override
    public WxCommentListVo getCommentList(WxCommentListBo commentListBo) {
        Byte showType = commentListBo.getShowType();
        WxCommentListVo commentListVo = new WxCommentListVo();

        PageHelper.startPage(commentListBo.getPage(), commentListBo.getSize());

        //挑选出符合条件的Comment
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(commentListBo.getValueId()).andTypeEqualTo(commentListBo.getType()).andDeletedEqualTo(false);
        if (showType==0){
            criteria.andHasPictureEqualTo(true);
        }
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        //分页信息
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();
        commentListVo.setCount((int) total);
        commentListVo.setCurrentPage(commentListBo.getPage());

        //将符合条件的Comment信息封装到DataBean中，加入dataBeans数组
        ArrayList<WxCommentListVo.DataBean> dataBeans = new ArrayList<>();
        for (Comment comment : comments) {
            WxCommentListVo.DataBean dataBean = new WxCommentListVo.DataBean();
            dataBean.setAddTime(comment.getAddTime());
            dataBean.setContent(comment.getContent());
            dataBean.setPicList(stringToList(comment.getPicUrls()));
            WxCommentListVo.DataBean.UserInfoBean userInfoBean = new WxCommentListVo.DataBean.UserInfoBean();
            Integer userId = comment.getUserId();
            OrderDetailByUserVo user = userMapper.selectUserById(userId);
            userInfoBean.setAvatarUrl(user.getAvatar());
            userInfoBean.setNickName(user.getNickname());
            dataBean.setUserInfo(userInfoBean);
            dataBeans.add(dataBean);
        }
        commentListVo.setData(dataBeans);

        return commentListVo;
    }
}
